﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Web;

namespace TireService.Web.UserControls.StatusOverview
{
    public partial class ucServiceScheduleBacklogOverview : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                TireServiceDB db = new TireServiceDB();

                BindServiceScheduleBacklogData(db);
            }
        }

        /// <summary>
        /// Bind chart data
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindServiceScheduleBacklogData(TireServiceDB db)
        {
            List<ServiceCar> servicecars = db.GetServiceCars();

            foreach (ServiceCar servicecar in servicecars)
            {
                // Add series for each servicecar
                AddChartSerie(db, servicecar);
            }
        }

        private void AddChartSerie(TireServiceDB db, ServiceCar servicecar)
        {
            // Add first series - Customer Portfolio
            // Create an empty Bar series and add it to the chart.
            Series series = new Series(servicecar.Description, ViewType.Bar);
            chartServiceScheduleBacklog.Series.Add(series);

            var servicecount = from p in db.ServiceCarScheduleDetails
                               where p.ServiceCarId == servicecar.ServiceCarId & p.ScheduleDate < DateTime.Now & p.ScheduleDetailTypeId == (int)Shared.ScheduleDetailType.ServiceDrive & p.WorkCompleted == false
                               group p by new
                               {
                                   p.ScheduleDate
                               }
                                   into grp
                                   select new
                                   {
                                       grp.Key.ScheduleDate,
                                       ScheduleDetailTypeCount = grp.Count()
                                   };

            // Generate a data table and bind the series to it.
            series.DataSource = servicecount;

            // Specify data members to bind the series.
            series.ArgumentScaleType = ScaleType.DateTime;
            series.ArgumentDataMember = "ScheduleDate";
            series.ValueScaleType = ScaleType.Numerical;
            series.ValueDataMembers.AddRange(new string[] { "ScheduleDetailTypeCount" });
        }

        /// <summary>
        /// Callback function that refreshes Chart
        /// </summary>
        protected void chartServiceScheduleBacklog_CustomCallback(object sender, CustomCallbackEventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            BindServiceScheduleBacklogData(db);
        }
    }
}